<template>
  <div class="main-content">
    <el-card
      class="mb-4"
      style="
        max-height: calc(100vh - 120px);
        padding: 5px auto;
        overflow: hidden;
      "
    >
      <!-- 统计卡片 -->
      <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
        <el-card class="text-center p-4">
          <div class="text-3xl font-bold text-primary">{{ totalCount }}</div>
          <div class="text-gray-500">总触发器数</div>
        </el-card>
        <el-card class="text-center p-4">
          <div class="text-3xl font-bold text-success">{{ activeCount }}</div>
          <div class="text-gray-500">活跃触发器</div>
        </el-card>
        <el-card class="text-center p-4">
          <div class="text-3xl font-bold text-info">{{ typeCount }}</div>
          <div class="text-gray-500">触发器类型</div>
        </el-card>
      </div>

      <!-- 筛选区域 -->
      <el-form
        ref="formRef"
        :inline="true"
        :model="form"
        class="search-form bg-bg_color w-full mb-4"
      >
        <el-form-item label="类型筛选：" prop="triggerType">
          <el-select
            v-model="form.triggerType"
            placeholder="全部类型"
            clearable
            class="w-[180px]!"
          >
            <el-option label="设备状态变化" value="device_status_change" />
            <el-option label="用户登录" value="user_login" />
            <el-option label="频道事件" value="channel_event" />
            <el-option label="定时触发" value="timed_trigger" />
          </el-select>
        </el-form-item>
        <el-form-item label="租户筛选：" prop="tenantId">
          <el-input
            v-model="form.tenantId"
            placeholder="全部租户"
            clearable
            class="w-[180px]!"
          />
        </el-form-item>
        <el-form-item label="状态筛选：" prop="status">
          <el-select
            v-model="form.status"
            placeholder="全部状态"
            clearable
            class="w-[180px]!"
          >
            <el-option label="活跃" value="1" />
            <el-option label="停用" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :icon="useRenderIcon('ri/search-line')"
            :loading="loading"
            @click="onSearch"
          >
            搜索
          </el-button>
          <el-button
            :icon="useRenderIcon('ep/refresh')"
            @click="resetForm(formRef)"
          >
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 表格区域 -->
      <PureTableBar title="触发器管理" :columns="columns" @refresh="onSearch">
        <template #buttons>
          <el-button
            type="primary"
            :icon="useRenderIcon('ri/add-circle-line')"
            @click="openDialog()"
          >
            添加触发器
          </el-button>
        </template>
        <template v-slot="{ size, dynamicColumns }">
          <div
            v-if="selectedNum > 0"
            v-motion-fade
            class="bg-[var(--el-fill-color-light)] w-full h-[46px] mb-2 pl-4 flex items-center"
          >
            <div class="flex-auto">
              <span
                style="font-size: var(--el-font-size-base)"
                class="text-[rgba(42,46,54,0.5)] dark:text-[rgba(220,220,242,0.5)]"
              >
                已选 {{ selectedNum }} 项
              </span>
              <el-button type="primary" text @click="onSelectionCancel">
                取消选择
              </el-button>
            </div>
            <el-popconfirm title="是否确认删除?" @confirm="onbatchDel">
              <template #reference>
                <el-button type="danger" text class="mr-1!">
                  批量删除
                </el-button>
              </template>
            </el-popconfirm>
          </div>
          <pure-table
            ref="tableRef"
            row-key="id"
            adaptive
            :adaptiveConfig="{ offsetBottom: 80 }"
            align-whole="center"
            table-layout="auto"
            :loading="loading"
            :size="size"
            :data="dataList"
            :columns="dynamicColumns"
            :pagination="{ ...pagination, size }"
            :header-cell-style="{
              background: 'var(--el-fill-color-light)',
              color: 'var(--el-text-color-primary)'
            }"
            @selection-change="handleSelectionChange"
            @page-size-change="handleSizeChange"
            @page-current-change="handleCurrentChange"
          >
            <template #operation="{ row }">
              <el-button
                class="reset-margin"
                link
                type="primary"
                :size="size"
                @click="handleTest(row)"
              >
                测试
              </el-button>
              <el-button
                class="reset-margin"
                link
                type="primary"
                :size="size"
                :icon="useRenderIcon('ep/edit-pen')"
                @click="openDialog('修改', row)"
              >
                编辑
              </el-button>
              <el-popconfirm
                :title="`是否确认删除触发器${row.triggerName}的这条数据`"
                @confirm="handleDelete(row)"
              >
                <template #reference>
                  <el-button
                    class="reset-margin"
                    link
                    type="primary"
                    :size="size"
                    :icon="useRenderIcon('ep/delete')"
                  >
                    删除
                  </el-button>
                </template>
              </el-popconfirm>
            </template>
          </pure-table>
        </template>
      </PureTableBar>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import { useTrigger } from "./utils/hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";

defineOptions({
  name: "SystemTrigger"
});

const tableRef = ref();
const formRef = ref();

const {
  form,
  loading,
  columns,
  dataList,
  pagination,
  selectedNum,
  totalCount,
  activeCount,
  typeCount,
  deviceDetection,
  onSearch,
  resetForm,
  onbatchDel,
  openDialog,
  handleTest,
  handleDelete,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  onSelectionCancel
} = useTrigger(tableRef);
</script>

<style lang="scss" scoped>
::v-deep.el-card {
  --el-card-padding: 10px 20px 0px 20px;
}

::v-deep.el-pagination {
  margin: 0;
}

.main-content {
  height: 90%;
  margin: 24px 24px 0 !important;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
